<template>
  <a-radio-group v-if="tagType=='radio'" @change="handleInput" :value="value" :disabled="disabled">
    <a-radio v-for="(item, key) in dictOptions" :key="key" :value="item.value">{{ item.text }}</a-radio>
  </a-radio-group>

  <a-select v-else-if="tagType=='select'" :placeholder="placeholder" :disabled="disabled" :allowClear="allowClear"
            :value="value" @change="handleInput" showSearch optionFilterProp="children" :filterOption="filterOption">
    <a-select-option value="">请选择</a-select-option>
    <a-select-option v-for="(item, key) in dictOptions" :key="key" :value="item.value">
      {{ item.text || item.label }}
    </a-select-option>
  </a-select>
</template>

<script>
import {ajaxGetDictItems} from '@/api/api'

export default {
  name: "JDictSelectTag",
  props: {
    dictCode: String,
    placeholder: String,
    triggerChange: Boolean,
    disabled: Boolean,
    value: [String, Number],
    type: String,
    allowClear: {
      type: Boolean,
      default: true
    },
    isCycleStatus:{
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      dictOptions: [],
      tagType: ""
    }
  },
  created() {
    if (!this.type || this.type === "list") {
      this.tagType = "select"
    } else {
      this.tagType = this.type
    }
    //获取字典数据
    this.initDictData();
  },
  methods: {
    initDictData() {
      //根据字典Code, 初始化字典数组
      ajaxGetDictItems(this.dictCode, null).then((res) => {
        if (res.success) {
          if(this.isCycleStatus) {
            this.dictOptions = res.result.filter(item => {
              return item.value != '2'
            })
          } else {
            this.dictOptions = res.result;
          }
        }
      })
    },
    handleInput(e) {
      let val;
      if (this.tagType == "radio") {
        val = e.target.value
      } else {
        val = e
      }
      if (this.triggerChange) {
        this.$emit('change', val);
      } else {
        this.$emit('input', val);
      }
    },
    setCurrentDictOptions(dictOptions) {
      this.dictOptions = dictOptions
    },
    getCurrentDictOptions() {
      return this.dictOptions
    },
    filterOption(input, option) {
      return option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0
    },
  }
}
</script>

<style scoped>
</style>